<?php
/**
 * Created by PhpStorm.
 * User: hooshim
 * Date: 2014-12-03
 * Time: 오전 11:23
 */

header("Content-Type:text/html;charset=utf-8");
header("Cache-Control:no-cache");
header("Pragma:no-cache");

include_once ($_SERVER['DOCUMENT_ROOT']."/inc/conn.inc.php");
include_once ($_SERVER['DOCUMENT_ROOT']."/inc/function.inc.php"); ##property 파일 인크루드됨.

	//## 파라미터 선언
    $findCompany 			= $_POST['findCompany'];

	// Table 이름 선언
	$tbname = "tblIncome";

    $curDt =date("Y-m-d");

    $query = "SELECT A.incomeDt, C.companyKR, CASE WHEN ISNULL(B.amount,0)>0 THEN '<img src=\"/img/inv.png\">' END as invoice ";
    $query .= ", REPLACE(CONVERT(varchar, CAST(A.amount AS money), 1), '.00', '') amount ";
    $query .= " , REPLACE(CONVERT(varchar, CAST(A.orgAmount AS money), 1), '.00', '') orgAmount, REPLACE(CONVERT(varchar, CAST(A.tax AS money), 1), '.00', '') tax ";
    $query .= " , RIGHT(D.actionDt,5) actionDt,  REPLACE(CONVERT(varchar, CAST(D.amount AS money), 1), '.00', '') incAmount ";
    $query .= " , RIGHT(D.bankDt,5) bankDt, A.income, A.billCd, CAST(A.incCd as varchar(10))+'^'+CAST(D.incSeq as varchar(10)) as LINK ";
    $query .= " FROM {$tbname} A LEFT JOIN (SELECT * FROM tblInvoice WHERE flag='Y') B ON A.invoiceCd=B.invoiceCd ";
	$query .= " LEFT JOIN (SELECT * FROM tblIncResult WHERE flag='Y') D ON A.incCd=D.incCd ";
	$query .= " LEFT JOIN tblCompany C ON A.compCd=C.compCd ";
    $query .= "		WHERE A.flag='Y' " .$condition;
	$query .= " ORDER BY A.incomeDt DESC";

    $logger->debug($query ."<br>[#FROM:" . $G_remote_ip."#]"); ## 쿼리 로그 작성
    $result = makeJSONArray($query,"GRIDDATA");

    $query = "SELECT REPLACE(CONVERT(varchar, CAST(SUM(amt) AS money), 1), '.00', '') sumAmt ";
    $query .= "         , REPLACE(CONVERT(varchar, CAST(SUM(orgAmt) AS money), 1), '.00', '') sumOrgAmt ";
    $query .= "         , REPLACE(CONVERT(varchar, CAST(SUM(taxAmt) AS money), 1), '.00', '') sumTax ";
    $query .= "         , REPLACE(CONVERT(varchar, CAST(SUM(incAmt) AS money), 1), '.00', '') sumIncAmt FROM ( ";
    $query .= "		SELECT SUM(A.amount) amt, SUM(A.orgAmount) orgAmt, SUM(A.tax) taxAmt, 0 incAmt ";
    $query .= "			 FROM {$tbname} A ";
    $query .= "		WHERE A.flag='Y' " .$condition;
    $query .= "		UNION ALL ";
    $query .= "		SELECT 0 amt, 0 orgAmt, 0 taxAmt, SUM(B.amount) incAmt ";
    $query .= "          FROM tblIncome A LEFT JOIN tblIncResult B ON  A.incCd=B.incCd ";
    $query .= "		WHERE A.flag='Y' AND B.flag='Y' " .$condition;
    $query .= ") P ";

    $logger->debug($query ."<br>[#FROM:" . $G_remote_ip."#]"); ## 쿼리 로그 작성
    $sum = makeJSONArray($query,$tbname);
    $result['SUM'] = $sum;


    $output = json_encode($result);
    $output = urldecode($output);
    echo iconvDecode($output);
    $logger->debug($_SESSION['BID'] ."//" .$output ."<br>[#FROM:" . $G_remote_ip."#]"); ## 결과 로그 작성
?>